package ua.vip.easyportfolio.web;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.gdata.data.photos.AlbumEntry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import ua.vip.easyportfolio.service.IPhotoService;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * User: Viktor M. Pishchulin
 * Date: 22 feb 2010
 * Time: 22:50:27
 */
@Controller
public class EasyPortfolioController {
    private static final Logger LOGGER = LoggerFactory.getLogger(EasyPortfolioController.class);
    private IPhotoService photoService;

    @Autowired
    public void setPhotoService(IPhotoService photoService) {
        this.photoService = photoService;
    }

    @RequestMapping("/")
    public String welcomeHandler() {
        return "main";
    }

    @RequestMapping("/albums")
    public ModelAndView albumsHandler() {
        Collection<AlbumEntry> albumEntries = photoService.getAlbums("viktorp");
        LOGGER.debug("Found: " + albumEntries);
        ModelAndView modelAndView = new ModelAndView("albums");
        modelAndView.addObject("albumEntries", albumEntries);
        return modelAndView;
    }

    @RequestMapping("/contacts")
    public ModelAndView contactsHandler() {
        return new ModelAndView("contacts");
    }

    @RequestMapping("/admin/configuration")
    public ModelAndView manageProfile() {
        LOGGER.debug("manageProfile");
        User user = UserServiceFactory.getUserService().getCurrentUser();
        Map<String, Object> map = new HashMap<String, Object>();
        if (user!=null) {
            LOGGER.debug(user.toString());
            ModelAndView modelAndView = new ModelAndView("configuration");
            modelAndView.addObject(user);
            return modelAndView;
        } else {
            return new ModelAndView("permissionDenied");
        }
    }
}
